var noPoint = "public/manager/images/noPoint.png";
var onePoint = "public/manager/images/onePoint.png";
var doorPoint = "public/images/door.png";
var currentEl = null;
var elActivated = new Array;
var elDesactivated = new Array;

var nameMenu = "#menutaches";
var menuYloc = null;


$(function() {
	$("#menutaches").hide();
	
	$("#block_type").change(
		function ()
		{
			if ($(this).val()== "door")
			{
				$(currentEl).find('img').attr('src', doorPoint);
				loadDoor();
			} else {
				$(currentEl).find('img').attr('src', onePoint);
				loadPassage();
			}
		}
	);
	
	menuYloc = 150;
	/**
	parseInt($(nameMenu).css("top").substring(0,
			$(nameMenu).css("top").indexOf("px")));
	*/
	$(window).scroll(function() {
		offset = menuYloc + $(document).scrollTop() + "px";
		$(nameMenu).animate( {
			top : offset
		}, {
			duration : 500,
			queue : false
		});
	});
	
	$(".tdMapView").dblclick(function() {
		$("#menutaches").show();
		var currPoint = $(this).find('img').attr('src');
		if (noPoint == currPoint) {
			$(this).find('img').attr('src', onePoint);
			addInArray(this, 'add');
			fnOnePoint(this);
			// $(this).find('img').attr('src', onePoint);
		} else {
			$(this).find('img').attr('src', noPoint);
			addInArray(this, 'del');
			fnNoPoint(this);
			// $(this).find('img').attr('src', noPoint);
		}

		return false;
	});

	$(".tdMapView").click(function() {

		$("#menutaches").show();
		var currPoint = $(this).find('img').attr('src');
		if (noPoint == currPoint) {
			fnNoPoint(this);
			// $(this).find('img').attr('src', onePoint);
		} else {
			fnOnePoint(this);
			// $(this).find('img').attr('src', noPoint);
		}

		return false;
	});

});

function fnNoPoint(el) {
	$(".tdMapView").css("border", "none");
	$(".tdMapView").css("width", "50px");
	$(".tdMapView").css("height", "50px");
	$("#mapMenu").hide();
	$("#mapView").find("img").css("width", "50px");
	$("#mapView").find("img").css("height", "50px");
	
	$(el).css("border", "2px solid");
	$(el).css("width", "46px");
	$(el).css("height", "46px");
	$(el).find("img").css("width", "46px");
	$(el).find("img").css("height", "46px");
	
	currentEl = el;
	$("#menutaches").show();
	var ul = $("#menutaches").find('ul');
	$(ul).empty();
	$(ul).html('<li><a href="#" id="mnu_actif">Rendre actif</a></li>');
	$("#mnu_actif").click(function() {
		$(currentEl).find('img').attr('src', onePoint);
		addInArray(currentEl, 'add');
		fnOnePoint(currentEl);
		return false;
	});

}

function fnOnePoint(el) {
	currentEl = el;
	$(".tdMapView").css("border", "none");
	$(".tdMapView").css("width", "50px");
	$(".tdMapView").css("height", "50px");
	$("#mapView").find("img").css("width", "50px");
	$("#mapView").find("img").css("height", "50px");
	$("#mapMenu").show();
	currImg = $(el).find('img').attr('src');
	if (currImg == doorPoint)
	{
		$("#block_type").val("door");
		loadDoor();
	}else if (currImg == onePoint) {
		$("#block_type").val("");
		loadPassage();
	} else  {
		$("#block_type").val("");
		$("#door_access").html("");
	}
	
	$(el).css("border", "2px solid");
	$(el).css("width", "46px");
	$(el).css("height", "46px");
	$(el).find("img").css("width", "46px");
	$(el).find("img").css("height", "46px");
	
	$("#menutaches").show();
	var ul = $("#menutaches").find('ul');
	$(ul).html('<li><a href="#" id="mnu_inactif">Rendre inactif</a></li>');
	$("#mnu_inactif").click(function() {
		$(currentEl).find('img').attr('src', noPoint);
		addInArray(currentEl, 'del');
		fnNoPoint(currentEl);
		return false;
	});
}

function addInArray(el, myAction) {
	var arrayAdd = null;
	var arrayRem = null;

	if (myAction == "add") {
		arrayAdd = elActivated;
		arrayRem = elDesactivated;
	} else {
		arrayAdd = elDesactivated;
		arrayRem = elActivated;
	}

	if (arrayRem.length != 0) {
		arrayRemTmp = new Array;
		for ( var i = 0; i < arrayRem.length; i++) {
			if (arrayRem[i] != $(el).attr('id'))
				arrayRemTmp.push(arrayRem[i]);
		}
		arrayRem = arrayRemTmp;
	}

	var inArray = false;
	if (arrayAdd.length != 0) {
		for ( var i = 0; i < arrayAdd.length && inArray == false; i++) {
			inArray = (arrayAdd[i] == $(el).attr('id'));
		}
	}
	if (!inArray)
		arrayAdd.push($(el).attr('id'));

	if (myAction == "add") {
		elActivated = arrayAdd;
		elDesactivated = arrayRem;
	} else {
		elActivated = arrayRem;
		elDesactivated = arrayAdd;
	}
	$("#desactivPoint").val('');
	$("#activPoint").val('');
	for ( var i = 0; i < elDesactivated.length; i++)
		$("#desactivPoint").val(
				$("#desactivPoint").val() + elDesactivated[i] + ';');
	for ( var i = 0; i < elActivated.length; i++)
		$("#activPoint").val($("#activPoint").val() + elActivated[i] + ';');
	return;
}

function loadPassage()
{
	var mapId = $("#mapId").val();
	//alert(mapId);
	var currPoint = $(currentEl).attr("id");
	//alert(currPoint);
	var urlLoad = "index.php?action=mapEditor&id="+mapId+"&actionmod=mapPassageView&door_info="+currPoint;
	//alert(urlLoad);
	$("#door_access").load(urlLoad, {}, function ()
	{
		$("#mapPassageSave").click(
				function ()
				{
					return mapPassageSave();
				}
		
		);
	}
	);
	
}

function loadDoor()
{
	var mapId = $("#mapId").val();
	//alert(mapId);
	var currPoint = $(currentEl).attr("id");
	//alert(currPoint);
	var urlLoad = "index.php?action=mapEditor&id="+mapId+"&actionmod=mapDoorsList&door_info="+currPoint;
	//alert(urlLoad);
	$("#door_access").load(urlLoad, {}, function ()
	{
		$("#mapDoorSave").click(
			function ()
			{
				return mapDoorSave();
			}
		);
		$("select[id^='mapDoor_']").hide();
		$("#mapDoor_"+$("#listMaps").val()).show();
		$("#listMaps").change(
			function ()
			{
				$("select[id^='mapDoor_']").hide();
				$("#mapDoor_"+$(this).val()).show();
			}
		);
	}
	);
	
}

function mapPassageSave()
{
	var mapId = $("#mapId").val();
	var currPoint = $(currentEl).attr("id");
	var urlLoad = "index.php?action=mapEditor&id="+mapId+"&actionmod=mapPassageSave&door_info="+currPoint;
	alert(urlLoad);
	$.get(urlLoad, 
		function(data){
			alert(data);
		}
	);
	
}

function mapDoorSave()
{
	$("#mapListInfo").html();
	var mapIssue = $("#listMaps").val();
	var issueVal = 0;
	if (mapIssue > 0)
	{
		issueVal = $("#mapDoor_"+mapIssue).val();
	}
	
	var mapId = $("#mapId").val();
	var currPoint = $(currentEl).attr("id");
	var pointId = $("#currDoor").val();
	
	var urlLoad = "index.php?action=mapEditor&id="+mapId+"&actionmod=mapDoorsSave&door_info="+currPoint+"&point_id="+pointId+"&toPoint="+issueVal;
	alert(urlLoad);
	$("#mapListInfo").load(urlLoad);
	return false;
}